﻿using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CallCenter.Data;
using CallCenter.Data.BizUtilities;
using System.Configuration;
using System.Data;
using CallCenter.Security;
using CallCenter.Data.Models;
using CallCenter.Data.Misc;

namespace CallCenter.Web.Controllers
{
    public class TaskController : BaseController
    {
        // GET: Task
        public ActionResult Index()
        {
            return View();
        }


        public JsonResult GetTask(TaskViewModel model, int id)
        {
            var scratchPads = this.Bridge.TaskUtility.GetAllGrid(id);
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [OutputCache(Duration = 0, VaryByParam = "*")]
        public JsonResult GetAllDependentTask(int id, int productReqestId)
        {
            List<DependTaskViewModel> DependTaskViewModelList = new List<DependTaskViewModel>();
            DependTaskViewModel DependTaskViewModel = new DependTaskViewModel();
            DependTaskViewModel.DependtaskName = "None";
            DependTaskViewModel.id = 0;
            DependTaskViewModelList.Add(DependTaskViewModel);
            DependTaskViewModelList.AddRange(this.Bridge.TaskUtility.GetAllDependTasksByProductRequestExceptSelectedTask(id, productReqestId));

            foreach (var item in DependTaskViewModelList.Where(d => d.id != 0))
            {
                DependTaskViewModelList.RemoveAll(x => !this.Bridge.TaskUtility.GetAllDependTasksByProductRequest(item.id, productReqestId).Any(y => y.id == x.id));
            }

            var DependTasks = DependTaskViewModelList.OrderBy(d => d.id).ToList();
            return Json(DependTasks, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetTaskHistory(TaskHistoryVm model, int id)
        {
            var scratchPads = this.Bridge.TaskUtility.GetAllTaskDependenceHistory(id);
            return Json(scratchPads, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Createtask(TaskViewModel model)
        {
                   
            model.DependTaskId = this.Bridge.TaskUtility.GetDependTaskIdByName(model.DependtaskName);

            var returnMessage = this.Bridge.TaskUtility.InsertToTask(model); ;
            if (!returnMessage.Status)
                ModelState.AddModelError("Error", returnMessage.Message);
            else {
                return Json(new AjaxSuccess { Message = "Success" }, JsonRequestBehavior.AllowGet);
            }
            //}

            return Json(new AjaxFailure { Errors = ModelState.SerializeErrors }, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult DeleteTask(TaskViewModel model)
        {
           
            int id = model.id;
            this.Bridge.TaskUtility.DeleteTask(id);
            var returnMessage = "Success";
            return Json(returnMessage, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateTask(TaskViewModel model)
        {
            model.heldContactId = this.Bridge.ccContactUtility.GetHeldIdByName(model.heldContactName);
            model.statusId = this.Bridge.StatusUtility.GetStatusIdByName(model.statusName);
            model.DependTaskId = this.Bridge.TaskUtility.GetDependTaskIdByName(model.DependtaskName);
            var result = this.Bridge.TaskUtility.UpdateToTask(model);
            return Json(result, JsonRequestBehavior.AllowGet);
        }



    }
}